package com.duobao.view;
import java.awt.*;

import javax.swing.*;

import com.duobao.game.DuobaoSwingLaunch;
import com.duobao.gongneng.*;
import com.duobao.server.*;
import com.duobao.uitl.*;

import java.awt.event.*;
import java.lang.reflect.Constructor;
import java.util.Vector;
public class MainPanel extends JPanel implements MouseListener{

    DuobaoSwingLaunch game;
    Map heroMap;
    Vector vector;//用于分页
    int newPage;

    String zd[];



    JLabel jlb_ent[];//地图人物进来信息列表
    JLabel jlb_zhangduo[];//人物战斗信息列表
    JLabel jlb_MapName;//地图名称
    JLabel jlb_MapFunction[];//地图功能，地图功能
    JLabel jlb_niYdao;//你遇到了
    JLabel jlb_MapNPCS[];//NPC，玩家，NPC等人
    JLabel jlb_zheLiYou;//这里有
    JLabel jlb_wuPing[];//物品，物品，物品等物品
    JLabel jlb_xiZou;//请选择你的行走方向
    JLabel jlb_xiZouFX[];//东西南北
    JLabel jlb_exit[];//地图人物离开信息列表
    JLabel jlb_MapMiS[];//地图描述
    JLabel jlb_ZhuangTai;//状态选项
    JLabel jlb_HuanGuo;//环顾四周
    JLabel jlb_FangHui;//返回首页
    JLabel jlb_nextPage;//下一页
    JLabel jlb_previousPage;//上一页
    public MainPanel(DuobaoSwingLaunch game, int page)
    {
        vector=new Vector();
        this.newPage=page;
        this.game=game;
        this.heroMap=Maps.getMap(this.game.hero.getLocation());
        GridLayout gl=new GridLayout(16,1,1,1);
        this.setLayout(gl);

        zd=new String[]{};


        setzhangduoxx();


        setMapName();
        setMapFunction();
        setNpcName();
        setEquipName();
        setXizou();
        setMapMiS();
        setZhuangTai();
        setHuanGuo();
        setFangHui();
        setadd();

    }
    public void setzhangduoxx()//设置战斗信息
    {
        //getMapZD();
        //System.out.println(this.zdous.size());
        String Des[]=SplitString.splitString3(this.zd);
        int leng=Des.length;
        jlb_zhangduo=new JLabel[leng];

        for(int i=0;i<leng;i++)
        {
            jlb_zhangduo[i]=new JLabel();
            jlb_zhangduo[i].addMouseListener(this);
            jlb_zhangduo[i].setFont(myFont.f2);
            jlb_zhangduo[i].setText(Des[i]);
            JPanel jp1=new JPanel();
            jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
            jp1.add(jlb_zhangduo[i]);
            this.vector.add(jp1);
        }
    }
    public String toString()
    {
        return "MainPanel";
    }
    private void Cheshi()//测试分页功能
    {
        for(int i=0;i<15;i++)
        {
            this.vector.add(new JLabel("测试分页功能"));
        }
    }
    private void setMapFunction()
    {
        int FuncId[]=this.heroMap.getFuncId();
        int leng=FuncId.length;
        jlb_MapFunction=new JLabel[leng];
        JPanel jp2=new JPanel();
        jp2.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        for(int i=0;i<leng;i++)
        {
            jlb_MapFunction[i]=new JLabel();
            jlb_MapFunction[i].addMouseListener(this);
            jlb_MapFunction[i].setFont(myFont.f2);
            jlb_MapFunction[i].setForeground(Color.BLUE);
            jlb_MapFunction[i].setText("<html><u>"+GongnengManager.getFuncName(FuncId[i])+"</u>"+"&nbsp&nbsp"+"</html>");
            jp2.add(jlb_MapFunction[i]);
        }
        if(leng>0)
        {
            this.vector.add(jp2);
        }

    }
    private void setZhuangTai()
    {
        jlb_ZhuangTai=new JLabel();
        jlb_ZhuangTai.addMouseListener(this);
        jlb_ZhuangTai.setFont(myFont.f2);
        jlb_ZhuangTai.setForeground(Color.BLUE);
        jlb_ZhuangTai.setText("<html><u>"+"状态/选项"+"</u></html>");
        JPanel jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp1.add(jlb_ZhuangTai);
        this.vector.add(jp1);
    }
    private void setHuanGuo()
    {
        jlb_HuanGuo=new JLabel();
        jlb_HuanGuo.addMouseListener(this);
        jlb_HuanGuo.setFont(myFont.f2);
        jlb_HuanGuo.setForeground(Color.BLUE);
        jlb_HuanGuo.setText("<html><u>"+"环顾四方"+"</u></html>");
        JPanel jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp1.add(jlb_HuanGuo);
        this.vector.add(jp1);
    }
    private void setFangHui()
    {
        jlb_FangHui=new JLabel();
        jlb_FangHui.setFont(myFont.f2);
        jlb_FangHui.setForeground(Color.BLUE);
        jlb_FangHui.setText("<html><u>"+"返回首页"+"</u></html>");
        JPanel jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp1.add(jlb_FangHui);
        this.vector.add(jp1);
    }
    private void setMapMiS()
    {
        String Des[]=SplitString.splitString(this.heroMap.getDescribe());
        int leng=Des.length;
        jlb_MapMiS=new JLabel[leng];

        for(int i=0;i<leng;i++)
        {
            jlb_MapMiS[i]=new JLabel();
            jlb_MapMiS[i].addMouseListener(this);
            jlb_MapMiS[i].setFont(myFont.f2);
            jlb_MapMiS[i].setText(Des[i]);
            JPanel jp1=new JPanel();
            jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
            jp1.add(jlb_MapMiS[i]);
            this.vector.add(jp1);
        }

    }
    private void setadd()//加载JLabel
    {
        if(this.vector.size()<=this.newPage*15)
        {
            for(int i=((this.newPage-1)*15);i<this.vector.size();i++)
            {
                this.add((Component) this.vector.get(i));
            }
            if(this.newPage!=1)
            {
                jlb_previousPage=new JLabel();
                jlb_previousPage.setFont(myFont.f2);
                jlb_previousPage.setForeground(Color.BLUE);
                jlb_previousPage.setText("<html><u>"+"上一页"+"</u></html>");
                this.add(jlb_previousPage);
            }
        }else
        {
            for(int i=((this.newPage-1)*15);i<(this.newPage)*15;i++)
            {
                this.add((Component) this.vector.get(i));
            }
            JPanel jPanel=new JPanel();
            jPanel.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
            if(this.newPage!=1)
            {
                jlb_previousPage=new JLabel();
                jlb_previousPage.setFont(myFont.f2);
                jlb_previousPage.setForeground(Color.BLUE);
                jlb_previousPage.setText("<html><u>"+"上一页"+"</u></html>");
                jPanel.add(jlb_previousPage);
            }
            jlb_nextPage=new JLabel();
            jlb_nextPage.setFont(myFont.f2);
            jlb_nextPage.setForeground(Color.BLUE);
            jlb_nextPage.setText("<html><u>"+"下一页"+"</u></html>");
            jPanel.add(jlb_nextPage);
            this.add(jPanel);

        }

    }
    private void setXizou()
    {
        jlb_xiZou=new JLabel("请选择你的行走方向：");
        jlb_xiZou.addMouseListener(this);
        jlb_xiZou.setFont(myFont.f2);
        JPanel jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp1.add(jlb_xiZou);
        this.vector.add(jp1);

        jlb_xiZouFX=new JLabel[4];
        for(int i=0;i<4;i++)
        {
            int dir=this.heroMap.getDirection()[i];
            if(dir!=0)
            {
                jlb_xiZouFX[i]=new JLabel();
                jlb_xiZouFX[i].addMouseListener(this);
                jlb_xiZouFX[i].setFont(myFont.f2);
                jlb_xiZouFX[i].setForeground(Color.BLUE);
                jlb_xiZouFX[i].setText("<html><u>"+getFX(i,Maps.getMap(dir).getMapName())+"</u></html>");
                JPanel jp=new JPanel();
                jp.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
                jp.add(jlb_xiZouFX[i]);
                this.vector.add(jp);
            }
        }
    }
    private String getFX(int fx,String str)
    {
        String s=null;
        switch(fx)
        {
            case 0:
                s="北："+str+"↑";break;
            case 1:
                s="西："+str+"←";break;
            case 2:
                s="东："+str+"→";break;
            case 3:
                s="南："+str+"↓";break;
        }
        return s;
    }
    private void setMapName()
    {
        jlb_MapName=new JLabel(heroMap.getMapName());
        jlb_MapName.addMouseListener(this);
        jlb_MapName.setFont(myFont.f2);
        JPanel jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp1.add(jlb_MapName);
        this.vector.add(jp1);
    }
    private void setEquipName()
    {
        jlb_zheLiYou=new JLabel("这里有");
        jlb_zheLiYou.addMouseListener(this);
        jlb_zheLiYou.setFont(myFont.f2);
        String Name[]=SplitString.splitEquipName(this.getEquipName());
        int leng=Name.length;
        jlb_wuPing=new JLabel[leng];
        for(int i=0;i<leng;i++)
        {
            jlb_wuPing[i]=new JLabel();
            jlb_wuPing[i].addMouseListener(this);
            jlb_wuPing[i].setFont(myFont.f2);
            jlb_wuPing[i].setForeground(Color.BLUE);
            jlb_wuPing[i].setText("<html><u>"+Name[i]+"</u></html>");
        }

        JPanel jp2=new JPanel();
        jp2.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp2.add(jlb_zheLiYou);
        jp2.add(jlb_wuPing[0]);
        if(!this.getEquipName().equals(""))
        {
            this.vector.add(jp2);
            for(int i=1;i<leng;i++)
            {
                this.vector.add(jlb_wuPing[i]);
            }
        }
    }
    private String getEquipName()
    {
        String s="";
        for(int i=0;i<heroMap.getEquips().size();i++)
        {
            if(heroMap.getEquips().size()==1)
            {
                s=heroMap.getEquips().get(i).getMsName();
            }else
            {
                if(i==0)
                {
                    //第一个物品名字前不需要'，'
                    s=heroMap.getEquips().get(i).getName();
                }else if(i<17)
                {
                    s=s+"，"+heroMap.getEquips().get(i).getName();
                }else
                {
                    s=s+"等物品";
                    break;
                }
            }

        }
        return s;
    }
    private void setNpcName()
    {

        jlb_niYdao=new JLabel("你遇到了");
        jlb_niYdao.addMouseListener(this);
        jlb_niYdao.setFont(myFont.f2);
        String Name[]=SplitString.splitNPCName(this.getNpcName());
        int leng=Name.length;
        jlb_MapNPCS=new JLabel[leng];
        for(int i=0;i<leng;i++)
        {
            jlb_MapNPCS[i]=new JLabel();
            jlb_MapNPCS[i].addMouseListener(this);
            jlb_MapNPCS[i].setFont(myFont.f2);
            jlb_MapNPCS[i].setForeground(Color.BLUE);
            jlb_MapNPCS[i].setText("<html><u>"+Name[i]+"</u></html>");
        }

        JPanel jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp1.add(jlb_niYdao);
        jp1.add(jlb_MapNPCS[0]);
        if(!this.getNpcName().equals(""))
        {
            this.vector.add(jp1);
            for(int i=1;i<leng;i++)
            {
                this.vector.add(jlb_MapNPCS[i]);
            }
        }



    }
    private String getNpcName()
    {
        String s="";
        for(int i=0;i<heroMap.getNpcs().size();i++)
        {
            NPC npc=heroMap.getNpcs().get(i);
            if(npc!=this.game.hero)//不显示主角
            {
                if(i<17)
                {
                    s=s+"，"+npc.getName();
                }else
                {
                    s=s+"等人";
                    break;
                }

            }

        }
        s=s.replaceFirst("，","");
        return s;
    }
    public boolean iFText(JLabel jl)
    {
        boolean f=false;
        if(jl==jlb_MapName || jl==jlb_niYdao || jl==jlb_zheLiYou || jl==jlb_xiZou)
        {
            f=true;
        }else
        {
            for(int i=0;i<jlb_MapMiS.length;i++)
            {
                if(jl==jlb_MapMiS[i])
                {
                    f=true;
                    break;
                }
            }
        }
        return f;
    }
    private int iFXZFX(JLabel jl)
    {
        int  f=-1;
        for(int i=0;i<jlb_xiZouFX.length;i++)
        {
            if(jl==jlb_xiZouFX[i])
            {
                    f=i;
            }
        }
        return f;
    }
    private int iFgongneng(JLabel jl)
    {
        int  f=-1;
        for(int i=0;i<jlb_MapFunction.length;i++)
        {
            if(jl==jlb_MapFunction[i])
            {
                    f=i;
            }
        }
        return f;
    }
    private boolean iFWuPing(JLabel jl)
    {
        boolean  b=false;
        for(int i=0;i<jlb_wuPing.length;i++)
        {
            if(jl==jlb_wuPing[i])
            {
                    b=true;
                    break;
            }
        }
        return b;
    }
    private boolean iFNPC(JLabel jl)
    {
        boolean  b=false;
        for(int i=0;i<jlb_MapNPCS.length;i++)
        {
            if(jl==jlb_MapNPCS[i])
            {
                    b=true;
                    break;
            }
        }
        return b;
    }
    private void getMapZD()
    {
        for(int i=0;i<this.heroMap.getNpcs().size();i++)
        {
            NPC npc=this.heroMap.getNpcs().get(i);
            if(npc.getZD()!= null)
            {
                //this.zdous.add(npc.getZD());
            }
        }
    }
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        int FX=iFXZFX((JLabel)e.getSource());
        int GN=iFgongneng((JLabel)e.getSource());
        if(FX!=-1)
        {
            this.game.hero.YiDong(FX);
            this.game.setPanel(new MainPanel(this.game,1));
        }else if(iFNPC((JLabel)e.getSource()))
        {
            String str=((JLabel)e.getSource()).getText();
            if(str.indexOf("，")==-1)
            {
                String name=str.substring(9, str.length()-11);
                NPC npc=this.heroMap.getNPC(name);
                if(npc!=null)
                {
                    this.game.setPanel(new NPCXinxi(this.game,npc,1));
                }else
                {

                    this.game.setPanel(new TishiNotFoutNPC(this.game));
                }
            }else
            {
                this.game.setPanel(new Renwu(this.game,1));
            }


        }else if(GN!=-1)
        {
            try {
                int id=this.heroMap.getFuncId()[GN];

                Constructor con=GongnengManager.getPabel(id);
                JPanel jl=(JPanel)(con.newInstance(this.game));
                this.game.setPanel(jl);
                Thread th = new Thread((Runnable)jl);
                th.start();
            } catch (Exception e1) {
                JOptionPane.showMessageDialog(null,"功能未添加");
                return;
            }
        }else if(iFWuPing((JLabel)e.getSource()))
        {
            this.game.setPanel(new Dishang(this.game,1));

        }else if(e.getSource()==jlb_HuanGuo)
        {
            //System.out.println("环顾");
            //System.out.println(this.heroMap.getNpcs().size());
            this.game.setPanel(new MainPanel(this.game,1));

        }else if(e.getSource()==jlb_ZhuangTai)
        {
            this.game.setPanel(new ZhuangTaiPanel(this.game,null,1));
        }
    }
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        if(iFText((JLabel)e.getSource()))
        {
            ((JLabel)e.getSource()).setCursor(new Cursor(Cursor.TEXT_CURSOR));
        }
    }
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }
}
